﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace MedicWave.Visualization3d
{
    partial class PlotSettingsForm : Form
    {
        //////////////////////////////////////////////////////////////////////////
        /// MEMBERS
        //////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// maximum rt value
        /// </summary>
        float m_fRTMax;
        /// <summary>
        /// minimum rt value
        /// </summary>
        float m_fRTMin;
        /// <summary>
        /// maximum mz value
        /// </summary>
        float m_fMzMax;
        /// <summary>
        /// minimum mz value
        /// </summary>
        float m_fMzMin;
        /// <summary>
        /// current rt from value
        /// </summary>
        float m_fCurRTFrom;
        /// <summary>
        /// current rt to value
        /// </summary>
        float m_fCurRTTo;
        /// <summary>
        /// current mz from value
        /// </summary>
        float m_fCurMzFrom;
        /// <summary>
        /// current mz to value
        /// </summary>
        float m_fCurMzTo;

        int m_iMaxRtResolution;
        int m_iMinRtResolution = 100;
        int m_iMaxMzResolution = 10000;
        int m_iMinMzResolution = 100;
        /// <summary>
        /// step that is used when changeing value via scrollbars 
        /// </summary>
        const int m_iStepSize = 10;

        
        Color originalBackColor;
        int nColors = 2;

        //////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////////
        /// METHODS
        //////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Initializes settings form
        /// </summary>
        public PlotSettingsForm()
        {
            InitializeComponent();

            originalBackColor = btnGrad3.BackColor;

            btnGrad3.MouseUp += new MouseEventHandler(button5_MouseClick);
            btnGrad4.MouseUp += new MouseEventHandler(button6_MouseClick);
            btnGrad5.MouseUp += new MouseEventHandler(button7_MouseClick);
            btnGrad6.MouseUp += new MouseEventHandler(button8_MouseClick);            
        }

        /// <summary>
        /// Event that occurs when cancel button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        /// <summary>
        /// Event that occurs when ok button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Event that occurs when color bottom button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnColor1_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnColorBottom.BackColor = colorDialogCtrl.Color;
            }
        }

        /// <summary>
        /// Event that occurs when color up button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnColor2_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnColorTop.BackColor = colorDialogCtrl.Color;
            }
        }

        /// <summary>
        /// Mehtod that shows settings form and sets it's initial data
        /// </summary> 
        /// <param name="fRtFrom">Minimal RT</param>
        /// <param name="fRtTo">Maximal RT</param>
        /// <param name="fMzFrom">Minimal Mz</param>
        /// <param name="fMzTo">Maximal Mz</param>
        /// <param name="ColorBottom">Color of lowest Intensity</param>
        /// <param name="ColorTop">Color of highest Intensity</param>
        /// <returns> Dialog Result if OK then we can chceck the fields</returns>
        public DialogResult ShowDialog(float fRtFrom, float fRtTo, float fMzFrom, float fMzTo, List<Color> colors, bool bGlobalIntensityAlpha, float fIntensityAlpha, bool bShowGrid)
        {
            // Rt info set
            txtboxRtFrom.Text = fRtFrom.ToString();
            m_fCurRTFrom = fRtFrom;
            m_fRTMin = fRtFrom;

            txtBoxRtTo.Text = fRtTo.ToString();
            m_fCurRTTo = fRtTo;
            m_fRTMax = fRtTo;

            groupBoxRT.Text += " Min: " + m_fRTMin.ToString() + "; Max: " + m_fRTMax.ToString();

            //Mz info set
            txtboxMzFrom.Text = fMzFrom.ToString();
            m_fCurMzFrom = fMzFrom;
            m_fMzMin = fMzFrom;

            txtBoxMzTo.Text = fMzTo.ToString();
            m_fCurMzTo = fMzTo;
            m_fMzMax = fMzTo;

            groupBoxMz.Text += " Min: " + m_fMzMin.ToString() + "; Max: " + m_fMzMax.ToString();

            // Events for text boxes RT
            txtboxRtFrom.Leave += new EventHandler(txtboxRtFrom_TextChanged);
            txtBoxRtTo.Leave += new EventHandler(txtBoxRtTo_TextChanged);

            txtBoxRtTo.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            txtboxRtFrom.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);

            // Values of scoll bars of RT
            ScrBrRtTo.Minimum = -1;
            ScrBrRtTo.Maximum = 2;
            ScrBrRtTo.SmallChange = 1;
            ScrBrRtTo.Value = 0;

            ScrBrRtFrom.Minimum = -1;
            ScrBrRtFrom.Maximum = 2;
            ScrBrRtFrom.SmallChange = 1;
            ScrBrRtFrom.Value = 0;

            // Events of scroll bars
            ScrBrRtTo.ValueChanged += new EventHandler(ScrollBarRtTo_ValueChanged);
            ScrBrRtFrom.ValueChanged += new EventHandler(ScrBrRtFrom_ValueChanged);

            // Values of scroll bars of Mz
            ScrBrMzTo.Minimum = -1;
            ScrBrMzTo.Maximum = 2;
            ScrBrMzTo.SmallChange = 1;
            ScrBrMzTo.Value = 0;

            ScrBrMzFrom.Minimum = -1;
            ScrBrMzFrom.Maximum = 2;
            ScrBrMzFrom.SmallChange = 1;
            ScrBrMzFrom.Value = 0;

            // Resolution
            txtMzResolution.Text = "300";
            txtMzResolution.KeyPress += new KeyPressEventHandler(txtMzResolution_KeyPress);
            ScrBrMzRes.ValueChanged += new EventHandler(ScrBrMzRes_ValueChanged);

            // Resolution
            txtRtResolution.Text = "300";
            txtRtResolution.KeyPress += new KeyPressEventHandler(txtMzResolution_KeyPress);
            ScrBrRtRes.ValueChanged += new EventHandler(ScrBrRtRes_ValueChanged);

            // Events of scroll bars
            ScrBrMzTo.ValueChanged += new EventHandler(ScrBrMzTo_ValueChanged);
            ScrBrMzFrom.ValueChanged += new EventHandler(ScrBrMzFrom_ValueChanged);

            // Events of text boxes MZ
            txtboxMzFrom.Leave += new EventHandler(txtboxMzFrom_Leave);
            txtBoxMzTo.Leave += new EventHandler(txtBoxMzTo_Leave);

            txtBoxMzTo.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            txtboxMzFrom.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);


            // Setings for Intensity
            filteringQuantileTextBOx.Text = "5,0";
            filteringQuantileTextBOx.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            filteringQuantileTextBOx.Leave += new EventHandler(txtboxIntTreshold_Leave);
            ScrBrIntGlobal.ValueChanged += new EventHandler(ScrBrIntTresh_ValueChanged);

           

            cmbBoxGradient.SelectedIndex = 0;
            //btnColorBottom.BackColor = ColorBottom;
            //btnColorTop.BackColor = ColorTop;
            btnGrad1.BackColor = colors[0];
            if (colors.Count > 1)
            {
                btnGrad2.BackColor = colors[1];
                btnGrad3.Visible = true;
            }
            if (colors.Count > 2)
            {
                btnGrad3.BackColor = colors[2];
                btnGrad4.Visible = true;
            }

            if (colors.Count > 3)
            {
                btnGrad4.BackColor = colors[3];
                btnGrad5.Visible = true;
            }

            if (colors.Count > 4)
            {
                btnGrad5.BackColor = colors[4];
                btnGrad6.Visible = true;
            }

            if (colors.Count > 5)
            {
                btnGrad6.BackColor = colors[5];
            }
            //if (bGlobalIntensityAlpha)
            //{
            //    radioIntensityGlobal.Checked = true;
            //    filteringQuantileTextBOx.Text = fIntensityAlpha.ToString();
            //}
            //else
            //{
            //    radioIntensityLocal.Checked = true;
            //    txtboxIntLocal.Text = fIntensityAlpha.ToString();
            //}
            nColors = colors.Count;
            chckBoxGrid.Checked = bShowGrid;
            //chckBoxLog.Checked = inte
            return base.ShowDialog();
        }

        public bool IsLogarithmicIntensity()
        {
            return chckBoxLog.Checked;
        }

        void ScrBrRtRes_ValueChanged(object sender, EventArgs e)
        {
            int value = int.Parse(txtRtResolution.Text);
            if (ScrBrRtRes.Value < 0)
            {
                value += m_iStepSize;
                if (value > m_iMaxRtResolution)
                    value = m_iMaxRtResolution;
            }
            else if (ScrBrRtRes.Value > 0)
            {
                value -= m_iStepSize;
                if (value < m_iMinRtResolution)
                    value = m_iMinRtResolution;
            }
            txtRtResolution.Text = value.ToString();
            ScrBrRtRes.Value = 0;
        }

        void ScrBrMzRes_ValueChanged(object sender, EventArgs e)
        {
            int value = int.Parse(txtMzResolution.Text);
            if (ScrBrMzRes.Value < 0)
            {
                value += m_iStepSize;
                if (value > m_iMaxMzResolution)
                    value = m_iMaxMzResolution;
            }
            else if (ScrBrMzRes.Value > 0)
            {
                value -= m_iStepSize;
                if (value < m_iMinMzResolution)
                    value = m_iMinMzResolution;
            }
            txtMzResolution.Text = value.ToString();
            ScrBrMzRes.Value = 0;
        }

        public bool ShowGrid
        {
            get
            {
                return chckBoxGrid.Checked;
            }
        }

        public bool OrthographicProjection
        {
            get
            {
                return orthographicCheckBox.Checked;
            }
        }

        public string PlotCaption
        {
            get
            {
                return plotCaptionBox.Text;
            }
        }
        void txtMzResolution_KeyPress(object sender, KeyPressEventArgs e)
        {
            string keyInput = e.KeyChar.ToString();
            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            if (Char.IsDigit(e.KeyChar))
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if (e.KeyChar == '\b')
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //MessageBeep();
            }
        }

       

        /// <summary>
        /// Event thath occurs when scroll bar of threshold is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScrBrIntTresh_ValueChanged(object sender, EventArgs e)
        {
            if (ScrBrIntGlobal.Value > 0)
            {
                float value = float.Parse(filteringQuantileTextBOx.Text);
                value -= m_iStepSize;
                if (value < 0)
                {
                    value += m_iStepSize;
                }
                filteringQuantileTextBOx.Text = value.ToString();
            }
            else if (ScrBrIntGlobal.Value < 0)
            {
                float value = float.Parse(filteringQuantileTextBOx.Text);
                value += m_iStepSize;
                if (value > 100)
                    value = 100.0f;
                filteringQuantileTextBOx.Text = value.ToString();
            }
            ScrBrIntGlobal.Value = 0;
        }

        /// <summary>
        /// Event that occurs after edditing the textbox of intensity
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtboxIntTreshold_Leave(object sender, EventArgs e)
        {
            float value = float.Parse(filteringQuantileTextBOx.Text);
            if (value < 0.0f)
            {
                filteringQuantileTextBOx.Text = "0,0";
            }
            else if (value > 100.0f)
            {
                filteringQuantileTextBOx.Text = "100,0";
            }
        }

        /// <summary>
        /// Event thath occurs when scroll bar of Mz from is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScrBrMzFrom_ValueChanged(object sender, EventArgs e)
        {
            if (ScrBrMzFrom.Value > 0)
            {
                m_fCurMzFrom = m_fCurMzFrom - m_iStepSize;
                if (m_fCurMzFrom < m_fMzMin)
                {
                    m_fCurMzFrom += m_iStepSize;
                }
                txtboxMzFrom.Text = m_fCurMzFrom.ToString();
            }
            else if (ScrBrMzFrom.Value < 0)
            {
                m_fCurMzFrom = m_fCurMzFrom + m_iStepSize;
                if (m_fCurMzFrom > m_fCurMzTo)
                {
                    m_fCurMzFrom -= m_iStepSize;
                }
                txtboxMzFrom.Text = m_fCurMzFrom.ToString();
            }
            ScrBrMzFrom.Value = 0;
        }

        /// <summary>
        /// Event that occurs when scroll bar of Mz to is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScrBrMzTo_ValueChanged(object sender, EventArgs e)
        {
            if (ScrBrMzTo.Value > 0)
            {
                m_fCurMzTo = m_fCurMzTo - m_iStepSize;
                if (m_fCurMzTo < m_fCurMzFrom)
                {
                    m_fCurMzTo += m_iStepSize;
                }
                txtBoxMzTo.Text = m_fCurMzTo.ToString();
            }
            else if (ScrBrMzTo.Value < 0)
            {
                m_fCurMzTo = m_fCurMzTo + m_iStepSize;
                if (m_fCurMzTo > m_fMzMax)
                {
                    m_fCurMzTo -= m_iStepSize;
                }
                txtBoxMzTo.Text = m_fCurMzTo.ToString();
            }
            ScrBrMzTo.Value = 0;
        }

        /// <summary>
        /// Event that occurs after editing the textbox of Mz to
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtBoxMzTo_Leave(object sender, EventArgs e)
        {
            float value = float.Parse(txtBoxMzTo.Text);
            if (value > m_fMzMax)
            {
                txtBoxMzTo.Text = m_fMzMax.ToString();
            }
            else if (value < m_fCurMzFrom)
            {
                txtBoxMzTo.Text = m_fCurMzFrom.ToString();
            }
            m_fCurMzTo = float.Parse(txtBoxMzTo.Text);
        }

        /// <summary>
        /// Event that occurs after edditing the textbox of Mz from
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtboxMzFrom_Leave(object sender, EventArgs e)
        {
            float value = float.Parse(txtboxMzFrom.Text);
            if (value < m_fMzMin)
            {
                txtboxMzFrom.Text = m_fMzMin.ToString();
            }
            else if (value > m_fCurRTTo)
            {
                txtboxMzFrom.Text = m_fCurMzTo.ToString();
            }
            m_fCurMzFrom = float.Parse(txtboxMzFrom.Text);
        }

        /// <summary>
        /// Event that occurs after edditing the textbox of RT to
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtBoxRtTo_TextChanged(object sender, EventArgs e)
        {
            float value = float.Parse(txtBoxRtTo.Text);
            if (value > m_fRTMax)
            {
                txtBoxRtTo.Text = m_fRTMax.ToString();
            }
            else if (value < m_fCurRTFrom)
            {
                txtBoxRtTo.Text = m_fCurRTFrom.ToString();
            }
            m_fCurRTTo = float.Parse(txtBoxRtTo.Text);
        }

        /// <summary>
        /// Event that occurs after edditing the textbox of Rt From
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtboxRtFrom_TextChanged(object sender, EventArgs e)
        {
            float value = float.Parse(txtboxRtFrom.Text);
            if (value < m_fRTMin)
            {
                txtboxRtFrom.Text = m_fRTMin.ToString();
            }
            else if (value > m_fCurRTTo)
            {
                txtboxRtFrom.Text = m_fCurRTTo.ToString();
            }
            m_fCurRTFrom = float.Parse(txtboxRtFrom.Text);
        }

        /// <summary>
        /// Event that checks input that provides only numeric data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtboxRtFrom_KeyPress(object sender, KeyPressEventArgs e)
        {
            string keyInput = e.KeyChar.ToString();
            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            if (Char.IsDigit(e.KeyChar))
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
         keyInput.Equals(negativeSign))
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if (e.KeyChar == '\b')
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //MessageBeep();
            }
        }

        /// <summary>
        /// Event thath occurs when scroll bar of Rt from is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScrBrRtFrom_ValueChanged(object sender, EventArgs e)
        {
            if (ScrBrRtFrom.Value > 0)
            {
                m_fCurRTFrom = m_fCurRTFrom - m_iStepSize;
                if (m_fCurRTFrom < m_fRTMin)
                {
                    m_fCurRTFrom += m_iStepSize;
                }
                txtboxRtFrom.Text = m_fCurRTFrom.ToString();
            }
            else if (ScrBrRtFrom.Value < 0)
            {
                m_fCurRTFrom = m_fCurRTFrom + m_iStepSize;
                if (m_fCurRTFrom > m_fCurRTTo)
                {
                    m_fCurRTFrom -= m_iStepSize;
                }
                txtboxRtFrom.Text = m_fCurRTFrom.ToString();
            }
            ScrBrRtFrom.Value = 0;
        }

        /// <summary>
        /// Event thath occurs when scroll bar of Rt to is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ScrollBarRtTo_ValueChanged(object sender, EventArgs e)
        {
            if (ScrBrRtTo.Value > 0)
            {
                m_fCurRTTo = m_fCurRTTo - m_iStepSize;
                if (m_fCurRTTo < m_fCurRTFrom)
                {
                    m_fCurRTTo += m_iStepSize;
                }
                txtBoxRtTo.Text = m_fCurRTTo.ToString();
            }
            else if (ScrBrRtTo.Value < 0)
            {
                m_fCurRTTo = m_fCurRTTo + m_iStepSize;
                if (m_fCurRTTo > m_fRTMax)
                {
                    m_fCurRTTo -= m_iStepSize;
                }
                txtBoxRtTo.Text = m_fCurRTTo.ToString();
            }
            ScrBrRtTo.Value = 0;
        }

        public bool GetSpecular()
        {
            return chckBoxSpecular.Checked;
        }
        /// <summary>
        /// Gets minimal RT from settings form 
        /// </summary>
        /// <returns>float</returns>
        public float GetMinRT()
        {
            return float.Parse(txtboxRtFrom.Text);
        }

        /// <summary>
        /// Gets maximal RT from settings form 
        /// </summary>
        /// <returns>float</returns>
        public float GetMaxRT()
        {
            return float.Parse(txtBoxRtTo.Text);
        }

        /// <summary>
        /// Gets minimal Mz from settings form 
        /// </summary>
        /// <returns>float</returns>
        public float GetMinMz()
        {
            return float.Parse(txtboxMzFrom.Text);
        }

        /// <summary>
        /// Gets maximal Mz from settings form 
        /// </summary>
        /// <returns>float</returns>
        public float GetMaxMz()
        {
            return float.Parse(txtBoxMzTo.Text);
        }

        /// <summary>
        /// Gets color of minimal Intensity from settings form 
        /// </summary>
        /// <returns>color</returns>
        public Color GetBottomColor()
        {
            return btnColorBottom.BackColor;
        }

        /// <summary>
        /// Gets color of maximal Intensity from settings form 
        /// </summary>
        /// <returns>color</returns>
        public Color GetTopColor()
        {
            return btnColorTop.BackColor;
        }

        /// <summary>
        /// Gets the intensity threshold (the below values will not be displayed
        /// </summary>
        /// <returns></returns>
        public float GetIntensityalfa(out bool isGlobal)
        {
            if (!filterIndividuallyCheckBox.Checked)
            {
                isGlobal = true;
                return float.Parse(filteringQuantileTextBOx.Text);
            }
            else
            {
                isGlobal = false;
                return float.Parse(filteringQuantileTextBOx.Text);
            }
        }

        /// <summary>
        /// Gets the factor of blending colors
        /// </summary>
        /// <returns>value betweean 0-1</returns>
        public float GetColorFactor()
        {
            return ((float)ScrBrRtFrom.Value) / 1000.0f;
        }


        public int GetGradientColorsNumber()
        {
            return nColors;
        }

        public int GetRtResolution()
        {
            return int.Parse(txtRtResolution.Text);
        }

        public int GetMzResolution()
        {
            return int.Parse(txtMzResolution.Text);
        }

        public Color GetGradientColor(int iColor)
        {
            if (iColor >= GetGradientColorsNumber())
                return Color.Black;

            if (iColor == 0)
                return btnGrad1.BackColor;
            else if (iColor == 1)
                return btnGrad2.BackColor;
            else if (iColor == 2)
                return btnGrad3.BackColor;
            else if (iColor == 3)
                return btnGrad4.BackColor;
            else if (iColor == 4)
                return btnGrad5.BackColor;
            else if (iColor == 5)
                return btnGrad6.BackColor;

            return Color.Black;

        }

        public bool GetOverlapLinesPlot()
        {
            return chckBoxOverlap.Checked;
        }
        public Color GetBackgroundColor()
        {
            return bkgColorButton.BackColor;
        }

        public bool GetPrinterColors()
        {
            return printerColorsCheckBox.Checked;
        }
        private void button5_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                btnGrad3.BackColor = originalBackColor;
                btnGrad3.UseVisualStyleBackColor = true;
                btnGrad4.Visible = false;
                btnGrad5.Visible = false;
                btnGrad6.Visible = false;
                nColors = 2;
            }

        }

        private void button6_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                btnGrad4.BackColor = originalBackColor;
                btnGrad4.UseVisualStyleBackColor = true;
                btnGrad5.Visible = false;
                btnGrad6.Visible = false;
                nColors = 3;

            }
        }

        private void button7_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                btnGrad5.BackColor = originalBackColor;
                btnGrad5.UseVisualStyleBackColor = true;
                btnGrad6.Visible = false;
                nColors = 4;
            }
        }

        private void button8_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                btnGrad6.BackColor = originalBackColor;
                btnGrad6.UseVisualStyleBackColor = true;
                nColors = 5;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad3.BackColor = colorDialogCtrl.Color;
                if (!btnGrad4.Visible)
                {

                    btnGrad4.Visible = true;
                    btnGrad4.BackColor = originalBackColor;
                    btnGrad4.UseVisualStyleBackColor = true;
                    nColors = 3;
                }

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad4.BackColor = colorDialogCtrl.Color;
                if (!btnGrad5.Visible)
                {
                    btnGrad5.Visible = true;
                    btnGrad5.BackColor = originalBackColor;
                    btnGrad5.UseVisualStyleBackColor = true;
                    nColors = 4;
                }

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad5.BackColor = colorDialogCtrl.Color;
                if (!btnGrad6.Visible)
                {
                    btnGrad6.Visible = true;
                    btnGrad6.BackColor = originalBackColor;
                    btnGrad6.UseVisualStyleBackColor = true;
                    nColors = 5;
                }

            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad6.BackColor = colorDialogCtrl.Color;
                nColors = 6;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad2.BackColor = colorDialogCtrl.Color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                btnGrad1.BackColor = colorDialogCtrl.Color;
            }
        }

        

      

        private void btnDefault_Click(object sender, EventArgs e)
        {
            txtboxMzFrom.Text = m_fMzMin.ToString();
            m_fCurMzFrom = m_fMzMin;
            txtBoxMzTo.Text = m_fMzMax.ToString();
            m_fCurMzTo = m_fMzMax;
            txtboxRtFrom.Text = m_fRTMin.ToString();
            m_fCurRTFrom = m_fRTMin;
            txtBoxRtTo.Text = m_fRTMax.ToString();
            m_fCurRTTo = m_fRTMax;
            txtRtResolution.Text = "300";
            txtMzResolution.Text = "300";
            //m_fCurMzFrom = m_fMzMin;
        }

        private void cmbBoxGradient_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbBoxGradient.SelectedIndex)
            {
                case 0:
                    btnGrad1.BackColor = Color.White;
                    btnGrad2.BackColor = Color.Red;
                    btnGrad3.Visible = true;
                    btnGrad3.BackColor = originalBackColor;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.Visible = false;
                    btnGrad5.Visible = false;
                    btnGrad6.Visible = false;
                    nColors = 2;
                    break;
                case 1:
                    btnGrad1.BackColor = Color.DarkGreen;
                    btnGrad2.BackColor = Color.Yellow;
                    btnGrad3.BackColor = Color.Orange;
                    btnGrad4.BackColor = Color.Red;
                    btnGrad3.Visible = true;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.Visible = true;
                    btnGrad5.Visible = true;
                    btnGrad5.BackColor = originalBackColor;
                    btnGrad6.Visible = false;
                    nColors = 4;
                    break;
                case 2:
                    btnGrad1.BackColor = Color.DarkGoldenrod;
                    btnGrad2.BackColor = Color.Blue;
                    //btnGrad3.BackColor = originalBackColor;
                    btnGrad3.BackColor = Color.Green;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.BackColor = Color.ForestGreen;
                    btnGrad3.Visible = true;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.Visible = true;
                    btnGrad5.Visible = true;
                    btnGrad5.BackColor = originalBackColor;
                    btnGrad6.Visible = false;
                    nColors = 4;
                    break;
                case 3:
                    btnGrad1.BackColor = Color.Violet;
                    btnGrad2.BackColor = Color.Blue;
                    //btnGrad3.BackColor = originalBackColor;
                    btnGrad3.BackColor = Color.Aqua;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.BackColor = Color.Green;
                    btnGrad5.BackColor = Color.Yellow;
                    btnGrad6.BackColor = Color.Red;
                    btnGrad3.Visible = true;
                    btnGrad4.Visible = true;
                    btnGrad5.Visible = true;
                    btnGrad6.Visible = true;
                    nColors = 6;
                    break;
                default:
                    btnGrad1.BackColor = Color.White;
                    btnGrad2.BackColor = Color.Red;
                    //btnGrad3.BackColor = originalBackColor;
                    btnGrad3.Visible = true;
                    btnGrad3.BackColor = originalBackColor;
                    //btnGrad5.UseVisualStyleBackColor = true;
                    btnGrad4.Visible = false;
                    btnGrad5.Visible = false;
                    btnGrad6.Visible = false;
                    nColors = 2;
                    break;
            }
        }


        internal DialogResult ShowDialog(InternalPlotSettings internalPlotSettings, int maxRTResolution, float minRt, float maxRt, float minMz, float maxMz)
        {
            printerColorsCheckBox.Checked = internalPlotSettings.ViewSettings.PrinterColors;
            bkgColorButton.BackColor = internalPlotSettings.ViewSettings.BkgColor;
            // Rt info set
            txtboxRtFrom.Text = internalPlotSettings.PlotSubsetDescription.m_fMinRetentionTime.ToString();
            m_fCurRTFrom = internalPlotSettings.PlotSubsetDescription.m_fMinRetentionTime;
            m_fRTMin = minRt;

            txtBoxRtTo.Text = internalPlotSettings.PlotSubsetDescription.m_fMaxRetentionTime.ToString();
            m_fCurRTTo = internalPlotSettings.PlotSubsetDescription.m_fMaxRetentionTime;
            m_fRTMax = maxRt;

            groupBoxRT.Text += " Min: " + m_fRTMin.ToString() + "; Max: " + m_fRTMax.ToString();

            //Mz info set
            txtboxMzFrom.Text = internalPlotSettings.PlotSubsetDescription.m_fMinMZ.ToString();
            m_fCurMzFrom = internalPlotSettings.PlotSubsetDescription.m_fMinMZ;
            m_fMzMin = minMz;

            txtBoxMzTo.Text = internalPlotSettings.PlotSubsetDescription.m_fMaxMZ.ToString();
            m_fCurMzTo = internalPlotSettings.PlotSubsetDescription.m_fMaxMZ;
            m_fMzMax = maxMz;

            groupBoxMz.Text += " Min: " + m_fMzMin.ToString() + "; Max: " + m_fMzMax.ToString();

            // Events for text boxes RT
            txtboxRtFrom.Leave += new EventHandler(txtboxRtFrom_TextChanged);
            txtBoxRtTo.Leave += new EventHandler(txtBoxRtTo_TextChanged);

            txtBoxRtTo.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            txtboxRtFrom.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);

            // Values of scoll bars of RT
            ScrBrRtTo.Minimum = -1;
            ScrBrRtTo.Maximum = 2;
            ScrBrRtTo.SmallChange = 1;
            ScrBrRtTo.Value = 0;

            ScrBrRtFrom.Minimum = -1;
            ScrBrRtFrom.Maximum = 2;
            ScrBrRtFrom.SmallChange = 1;
            ScrBrRtFrom.Value = 0;

            // Events of scroll bars
            ScrBrRtTo.ValueChanged += new EventHandler(ScrollBarRtTo_ValueChanged);
            ScrBrRtFrom.ValueChanged += new EventHandler(ScrBrRtFrom_ValueChanged);

            // Values of scroll bars of Mz
            ScrBrMzTo.Minimum = -1;
            ScrBrMzTo.Maximum = 2;
            ScrBrMzTo.SmallChange = 1;
            ScrBrMzTo.Value = 0;

            ScrBrMzFrom.Minimum = -1;
            ScrBrMzFrom.Maximum = 2;
            ScrBrMzFrom.SmallChange = 1;
            ScrBrMzFrom.Value = 0;

            // Resolution MZ            
            txtMzResolution.Text = internalPlotSettings.PlotSubsetDescription.m_iResolutionMZ.ToString();
            txtMzResolution.KeyPress += new KeyPressEventHandler(txtMzResolution_KeyPress);
            txtMzResolution.Leave += new EventHandler(txtMzResolution_Leave);
            ScrBrMzRes.ValueChanged += new EventHandler(ScrBrMzRes_ValueChanged);

            // Resolution RT
            m_iMaxRtResolution = maxRTResolution;
            txtRtResolution.Text = internalPlotSettings.PlotSubsetDescription.m_iResolutionRetentionTime.ToString();
            txtRtResolution.KeyPress += new KeyPressEventHandler(txtMzResolution_KeyPress);
            txtRtResolution.Leave += new EventHandler(txtRtResolution_Leave);
            ScrBrRtRes.ValueChanged += new EventHandler(ScrBrRtRes_ValueChanged);

            // Events of scroll bars
            ScrBrMzTo.ValueChanged += new EventHandler(ScrBrMzTo_ValueChanged);
            ScrBrMzFrom.ValueChanged += new EventHandler(ScrBrMzFrom_ValueChanged);

            // Events of text boxes MZ
            txtboxMzFrom.Leave += new EventHandler(txtboxMzFrom_Leave);
            txtBoxMzTo.Leave += new EventHandler(txtBoxMzTo_Leave);

            txtBoxMzTo.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            txtboxMzFrom.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);


            // Setings for Intensity
            filteringQuantileTextBOx.Text = "100,0";
            filteringQuantileTextBOx.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            filteringQuantileTextBOx.Leave += new EventHandler(txtboxIntTreshold_Leave);
            ScrBrIntGlobal.ValueChanged += new EventHandler(ScrBrIntTresh_ValueChanged);

           

            chckBoxOverlap.Checked = internalPlotSettings.OverlapLines;
            cmbBoxGradient.SelectedIndex = 0;
            //btnColorBottom.BackColor = ColorBottom;
            //btnColorTop.BackColor = ColorTop;
            btnGrad1.BackColor = internalPlotSettings.ColorGradient.Colors[0];
            if (internalPlotSettings.ColorGradient.Colors.Count > 1)
            {
                btnGrad2.BackColor = internalPlotSettings.ColorGradient.Colors[1];
                btnGrad3.Visible = true;
            }
            if (internalPlotSettings.ColorGradient.Colors.Count > 2)
            {
                btnGrad3.BackColor = internalPlotSettings.ColorGradient.Colors[2];
                btnGrad4.Visible = true;
            }

            if (internalPlotSettings.ColorGradient.Colors.Count > 3)
            {
                btnGrad4.BackColor = internalPlotSettings.ColorGradient.Colors[3];
                btnGrad5.Visible = true;
            }

            if (internalPlotSettings.ColorGradient.Colors.Count > 4)
            {
                btnGrad5.BackColor = internalPlotSettings.ColorGradient.Colors[4];
                btnGrad6.Visible = true;
            }

            if (internalPlotSettings.ColorGradient.Colors.Count > 5)
            {
                btnGrad6.BackColor = internalPlotSettings.ColorGradient.Colors[5];
            }
            if (internalPlotSettings.PlotSubsetDescription.m_bGlobalIntensity)
            {
                filterIndividuallyCheckBox.Checked = false;
                filteringQuantileTextBOx.Text = internalPlotSettings.PlotSubsetDescription.m_fIntensityTreshold.ToString();
            }
            else
            {
                filterIndividuallyCheckBox.Checked = true;
                filteringQuantileTextBOx.Text = internalPlotSettings.PlotSubsetDescription.m_fIntensityTreshold.ToString();
            }
            nColors = internalPlotSettings.ColorGradient.Colors.Count;
            chckBoxGrid.Checked = internalPlotSettings.ViewSettings.ShowGrid;

            if (internalPlotSettings.ViewSettings.ProjectionType == ProjectionType.Orthographic)
                orthographicCheckBox.Checked = true;
            else
                orthographicCheckBox.Checked = false;

            plotCaptionBox.Text = internalPlotSettings.ViewSettings.PlotTitle;
            chckBoxLog.Checked = internalPlotSettings.LogarithmicIntensity;
            chckBoxSpecular.Checked = internalPlotSettings.ViewSettings.UseSpecular;
            return base.ShowDialog();
        }

        void txtRtResolution_Leave(object sender, EventArgs e)
        {
            int value = int.Parse(txtRtResolution.Text);
            if (value < m_iMinRtResolution)
            {
                txtRtResolution.Text = m_iMinRtResolution.ToString();
            }
            else if (value > m_iMaxRtResolution)
            {
                txtRtResolution.Text = m_iMaxRtResolution.ToString();
            }
        }

        void txtMzResolution_Leave(object sender, EventArgs e)
        {
            int value = int.Parse(txtMzResolution.Text);
            if (value < m_iMinMzResolution)
            {
                txtMzResolution.Text = m_iMinMzResolution.ToString();
            }
            else if (value > m_iMaxMzResolution)
            {
                txtMzResolution.Text = m_iMaxMzResolution.ToString();
            }
        }

        private void bkgColorButton_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                bkgColorButton.BackColor = colorDialogCtrl.Color;
                
            }
        }
    }
}
